import Vue from 'vue'
import VueRouter from 'vue-router'
// 一级路由需要导入，二级路由可以懒加载
import Layout from '../views/layout';
import Login from '../views/login';
import session from '../utils/local';
Vue.use(VueRouter)

const routes = [
  {path:"/",redirect:"/login"},
  {path:"/login",component:Login},
  // 配置首页路由
  {path:"/home",meta:{title:"首页"},component:Layout,redirect:"/home",children:[
    {path:"",component:()=>import("../views/home")}
  ]},
]
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  let token=session.get("sell_token") ;
  if(token){
    next();
  }else{
    if(to.path=="/login"){
      next();
    }else{
      next("/login")
    }
  }
})
const menuList=[
  {path:"/",redirect:"/login",meta:{isMenu:'no'}},
  {path:"/login",component:Login,meta:{isMenu:'no'}},
  // 配置首页路由
  {path:"/home",meta:{title:"首页",icon:'icon-31shouyexuanzhong',level:1},component:Layout,redirect:"/home",children:[
    {path:"",component:()=>import("../views/home")}
  ]},
  // 配置用户路由
  {path:"/users",meta:{title:"账户管理",icon:'icon-zhanghaozhongxinzhanghaoguanli',level:2,roles:['super','normal']},component:Layout,redirect:"/users/list",children:[
    {path:"list",meta:{title:"账户列表",roles:['super']},component:()=>import("../views/users/UsersList.vue")},
    {path:"personal",meta:{title:"个人中心",roles:['super','normal']},component:()=>import("../views/users/UserPersonal.vue")},
    {path:"pwd",meta:{title:"修改密码",roles:['super','normal']},component:()=>import("../views/users/UserPwd.vue")},
    {path:"save",meta:{title:"添加账户",roles:['super']},component:()=>import("../views/users/UserSave.vue")}
  ]},
  // 配置订单路由
  {path:"/order",meta:{title:"订单管理",icon:'icon-dingdan1',level:2},component:Layout,redirect:"/order/list",children:[
    {path:"detail",meta:{title:"订单详情",isMenu:'no'},component:()=>import('../views/order/OrderDetail.vue')},
    {path:"list",meta:{title:"订单列表"},component:()=>import('../views/order/OrderList.vue')},
    {path:"update",meta:{title:"修改订单",isMenu:'no'},component:()=>import('../views/order/OrderUpdate.vue')},
  ]},
  // 统计
  {path:"/census",meta:{title:"统计管理",icon:'icon-tongjifenxi-changguitongji',level:2,roles:['super']},component:Layout,redirect:"/census/order",children:[
    {path:"order",meta:{title:"订单统计",roles:['super']},component:()=>import("../views/census/OrderCensus.vue")},
    {path:"goods",meta:{title:"商品统计",roles:['super']},component:()=>import("../views/census/GoodScensus.vue")}
  ]},
  // 商品
  {path:"/goods",meta:{title:"商品管理",icon:'icon-shangpin',level:2,roles:['super','normal']},component:Layout,redirect:"/goods/list",children:[
    {path:"list",meta:{title:"商品列表",roles:['super','normal']},component:()=>import("../views/goods/GoodsList.vue")},
    {path:"save",meta:{title:"添加商品",roles:['super']},component:()=>import("../views/goods/GoodsSave.vue")},
    {path:"type",meta:{title:"商品分类",roles:['super']},component:()=>import("../views/goods/GoodsType.vue")}
  ]},
  // 店铺
  {path:"/shop",meta:{title:"店铺管理",icon:'icon-dianpu',level:1,roles:['super']},component:Layout,redirect:"/shop/",children:[
    {path:"",component:()=>import("../views/shop/index.vue")}
  ]},
  {path:"/404",meta:{isMenu:'no'},component:()=>import("../views/404")},
  {path:"/*",meta:{isMenu:'no'},redirect:"/404"}
];
// 计算出一个完整的根据角色进行划分的数组，然后将这个数据添加到router中
// 定义一个计算函数，得到一个根据角色可以看到模块的数组
export function handler(){
  let role=session.get("sell_role"); //从本地获取当前登录人的角色信息
  if(!role){
    role="normal";
  }
  let menus=[];
  // 计算出当前角色所拥有的权限
  menuList.forEach((item)=>{
    if(isRole(item,role)){
      // 判断是否包含children，如果包含了children那么则处理二级
      let chr=[];
      if(item.children){
        item.children.forEach((obj)=>{
          if(isRole(obj,role)){
            chr.push(obj);
          }
        })
        item.children=chr;
      }
      menus.push(item);
    }
  })
  // 追加到router中
  router.addRoutes(menus);

  // 排除不渲染到目录中的菜单
  let mm=[];
  menus.forEach((item)=>{
    if(item.meta && item.meta.isMenu!="no"){
      if(item.children){
        let cc=[];
        item.children.forEach((obj)=>{
          if(obj.meta && obj.meta.isMenu!="no"){
            cc.push(obj);
          }
        })
        item.children=cc;
      }
     mm.push(item);
    }
  })

  // 将mm存放在本地
  session.set("sell_menus",mm);
}
handler();

// 定义一个方法用于判断当前这个对象是否需要添加到新的数组中
// function isRole(obj,role){
//   if(obj.meta && obj.meta.roles){
//     // 判断路由对象roles是否包含role['super','normal'],就是把角色划分
//    return obj.meta.roles.includes(role);
//   }else{
//     return true;
//   }
// }

function isRole(obj,role){
  if(obj.meta && obj.meta.roles){
   return obj.meta.roles.includes(role);
  }else{
    return true;
  }
}





export default router
